<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="inc/taglibs.jsp" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <%@ include file="inc/meta.jsp" %>
    <meta name="description" content="">
    <meta name="author" content="">
    <title>信件列表</title>
    <%@ include file="inc/css.jsp" %>
</head>

<body>
<div id="posts" class="wrapper">

    <%@ include file="inc/nav.jsp" %>

    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">信件列表</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <a href="javascript:void(0)" onclick="_letterList.fn.batchDel()" class="btn btn-outline btn-danger btn-lg" role="button">一键删除</a>
                        <a href="javascript:void(0)" onclick="_letterList.fn.modifyPrice()" class="btn btn-outline btn-info btn-lg" role="button">价格编辑</a>

                        <form class="navbar-form navbar-right" role="search">
                            <div class="form-group">
                                <input type="text" class="form-control" id="userName" maxlength="20" placeholder="用户名"/>
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control input-append date form_datetime" style="width: 180px;" readonly id="startTime" maxlength="20" placeholder="开始时间"/>
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control input-append date form_datetime" style="width: 180px;" readonly id="endTime" maxlength="20" placeholder="结束时间"/>
                            </div>
                            <div class="form-group">
                                <select class="form-control" id="typeList" style="width: 150px;">
                                    <option value="">全部类型</option>
                                    <option value="1">系统</option>
                                    <option value="2">陌生人</option>
                                    <option value="3">熟人</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <select class="form-control" id="statusList" style="width: 150px;">
                                    <option value="">全部</option>
                                    <option value="1">已打印</option>
                                    <option value="2">未打印</option>
                                </select>
                            </div>
                            <button type="button" id="c_search" class="btn btn-primary btn-sm">搜索</button>
                        </form>
                    </div>
                    <!-- /.panel-heading -->
                    <div class="panel-body">

                        <div class="table-responsive">

                            <table class="table table-striped table-bordered table-hover" id="dataTables">
                                <colgroup>
                                    <col class="gradeA even" style="width: 6%"/>
                                    <col class="gradeA odd"/>
                                    <col class="gradeA even"/>
                                    <col class="gradeA odd"/>
                                    <col class="gradeA even"/>
                                    <col class="gradeA odd"/>
                                    <col class="gradeA even"/>
                                </colgroup>
                                <thead>
                                <tr>
                                    <th><input type="checkbox" onclick="$leoman.checkAll(this)" class="checkall"/></th>
                                    <th>信件内容</th>
                                    <th>用户昵称</th>
                                    <th>发布时间</th>
                                    <th>信件类型</th>
                                    <th>打印状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>

                    </div>
                    <!-- /.panel-body -->

                </div>
                <!-- /.panel -->
            </div>
        </div>
    </div>
    <!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->

<div class="modal fade" id="infoModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">价格编辑</h4>
            </div>
            <div class="modal-body">
                <form id="infoForm" method="post" action="backend/prompt/save" class="form-horizontal" role="form" enctype="multipart/form-data">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">时光慢递:</label>
                        <div class="col-sm-5">
                            <input type="number" class="form-control" id="money" maxlength="20" value="${money}" placeholder="请输入价格"/>
                        </div>
                        <label class="col-sm-1 control-label">元</label>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">有个快递:</label>
                        <div class="col-sm-5">
                            <input type="number" class="form-control" id="money1" maxlength="20" value="${money1}" placeholder="请输入价格"/>
                        </div>
                        <label class="col-sm-1 control-label">元</label>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" onclick="_letterList.fn.subPriceInfo()" class="btn btn-primary">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<div class="modal fade" id="delModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">删除提示</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="id"/>
                确定删除该信件？
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" onclick="_letterList.fn.subDelInfo()" class="btn btn-primary">确定</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<%@ include file="inc/footer.jsp" %>
</body>

<script type="text/javascript">
    var _letterList = {
        v: {
            id: "_letterList",
            list: [],
            dTable: null,
            postId: 0,
            status: 0
        },
        fn: {
            init: function () {
                $('.form_datetime').datetimepicker({
                    language: 'zh-CN',
                    weekStart: 1,
                    todayBtn: 1,
                    autoclose: 1,
                    todayHighlight: 1,
                    startView: 2,
                    forceParse: 0,
                    showMeridian: 1,
                    format: 'yyyy-mm-dd hh:ii:ss'
                });

                _letterList.fn.dataTableInit();

                // 查询
                $("#c_search").click(function () {
                    _letterList.v.dTable.ajax.reload();
                });
            },
            dataTableInit: function () {
                _letterList.v.dTable = $leoman.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/letter/list",
                        "type": "POST"
                    },
                    "columns": [
                        {"data": "id"},
                        {
                            "data": null,
                            render: function (data) {
                                if (data.content.length > 20) {
                                    return data.content.substring(0, 20) + '...';
                                } else {
                                    return data.content;
                                }
                            }
                        },
                        {
                            "data": null,
                            render: function (data) {
                                if (null != data.user && null != data.user.nickName) {
                                    return data.user.nickName;
                                } else {
                                    return "管理员";
                                }
                            }
                        },
                        {
                            "data": "createTime",
                            render: function (data) {
                                return formatJsonDateTime(data, 'yyyy-MM-dd hh:mm:ss');
                            }
                        },
                        {
                            "data": "type",
                            render: function (data) {
                                var showName = "";
                                switch (data) {
                                    case 1:
                                        showName = '系统';
                                        break;
                                    case 2:
                                        showName = '陌生人';
                                        break;
                                    case 3:
                                        showName = '熟人';
                                        break;
                                }
                                return showName;
                            }
                        },
                        {
                            "data": "printStatus",
                            render: function (data) {
                                var showName = "";
                                switch (data) {
                                    case 0:
                                        showName = '未打印';
                                        break;
                                    case 1:
                                        showName = '已打印';
                                        break;
                                }
                                return showName;
                            }
                        },
                        {"data": ""}
                    ],
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='查看' class='btn btn-primary btn-circle look'>" +
                            "<i class='fa fa-eye'></i>" +
                            "</button>" +
                            "<button type='button' title='收信人列表' style='margin-left: 8px;display: none' class='btn btn-success btn-circle acceptList'>" +
                            "<i class='fa fa-table'></i>" +
                            "</button>" +
                            "<button type='button' title='置顶' style='margin-left: 8px;' class='btn btn-info btn-circle toTop'>" +
                            "<i class='fa fa-level-up'></i>" +
                            "</button>" +
                            "<button type='button' title='删除' style='margin-left: 8px;' class='btn btn-danger btn-circle delete'>" +
                            "<i class='fa fa-trash'></i>" +
                            "</button>",
                            "targets": -1
                        }
                    ],
                    "createdRow": function (row, data, index) {
                        _letterList.v.list.push(data);
                        $('td', row).eq(0).html("<input type='checkbox' value=" + data.id + ">");
                    },
                    rowCallback: function (row, data) {
                        if (data.isTop == 1) {
                            $('td', row).last().find(".toTop").css('display', 'none');
                        }

                        if (data.type == 2) {
                            $('td', row).last().find(".acceptList").css('display', '');
                        }

                        $('td', row).last().find(".look").click(function () {
                            window.location.href = 'backend/letter/add?letterId=' + data.id;
                        });

                        $('td', row).last().find(".acceptList").click(function () {
                            window.location.href = 'backend/letter/accept/index?letterId=' + data.id;
                        });

                        $('td', row).last().find(".toTop").click(function () {
                            _letterList.fn.toTop(data.id);
                        });

                        $('td', row).last().find(".delete").click(function () {
                            _letterList.fn.delInfo(data.id);
                        });
                    },
                    "fnServerParams": function (aoData) {
                        aoData.userName = $('#userName').val();
                        aoData.startTime = $('#startTime').val();
                        aoData.endTime = $('#endTime').val();
                        aoData.type = $('#typeList option:selected').val();
                        aoData.status = $('#statusList option:selected').val();
                    },
                    "fnDrawCallback": function (row) {
                        $leoman.uiform();
                    }
                });
            },
            delInfo: function (id) {
                $('#id').val(id);
                $("#delModal").modal("show");
            },
            subDelInfo: function () {
                var id = $('#id').val();

                $leoman.ajax("backend/letter/delete", {
                    "letterId": id
                }, function (result) {
                    if (result == 1) {
                        $leoman.notify("操作成功", "success");
                        $("#delModal").modal("hide");
                        _letterList.v.dTable.ajax.reload(null, false);
                    } else {
                        $leoman.notify("操作失败", "error");
                    }
                });
            },
            toTop: function (id) {
                $leoman.ajax("backend/letter/toTop", {
                    "letterId": id
                }, function (result) {
                    if (result == 1) {
                        $leoman.notify("操作成功", "success");
                        _letterList.v.dTable.ajax.reload(null, false);
                    } else {
                        $leoman.notify("操作失败", "error");
                    }
                });
            },
            batchDel: function () {
                var checkBox = $("#dataTables tbody tr").find('input[type=checkbox]:checked');
                var ids = checkBox.getInputId();
                _letterList.fn.deleteRow(checkBox, ids)
            },
            deleteRow: function (checkBox, ids) {
                if (ids.length > 0) {
                    $leoman.optNotify(function () {
                        $leoman.ajax("backend/letter/batchDelete", {
                            ids: JSON.stringify(ids)
                        }, function (result) {
                            if (result > 0) {
                                $leoman.notify("操作成功", "success");
                                _letterList.v.dTable.ajax.reload();
                            } else {
                                $leoman.notify("操作失败", "error");
                            }
                        })
                    }, '确定删除选中的所有信件？', '确定');
                }
            },
            modifyPrice: function () {
                $("#infoModal").modal("show");
            },
            subPriceInfo: function () {
                var flag = true;
                var money = $('#money').val();
                var money1 = $('#money1').val();

                if (null == money || money == '') {
                    $leoman.notify("请输入时光慢递价格", "error");
                    flag = false;
                    return;
                }

                if (null == money1 || money1 == '') {
                    $leoman.notify("请输入有个快递价格", "error");
                    flag = false;
                    return;
                }

                // 所有的验证通过后，执行保存操作
                if (flag) {
                    $leoman.ajax("backend/letter/updatePrice", {
                        "money": money,
                        "money2": money1
                    }, function (result) {
                        if (result == 1) {
                            $leoman.notify("操作成功", "success");
                            $("#infoModal").modal("hide");
                        } else {
                            $leoman.notify("操作失败", "error");
                        }
                    });
                }
            }
        }
    }

    $(document).ready(function () {
        _letterList.fn.init();
    });

</script>

</html>